package lock;

import ThreadBasic.Single.T;
import sun.security.krb5.internal.Ticket;

/**
 * @author yyh
 * @create 2020-11-09 14:52
 *
 *                synchronized
 */
public class SaleTicketDemo01 {

    private static Ticket t = new Ticket(); 

    public static void main(String[] args) {

        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                t.sale();
            }
        },"A").start();

        new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                t.sale();
            }
        },"B").start();


    }

    private static class Ticket{

        private int num = 30;

        private int i = 0;

        public void sale(){
            synchronized(t){
                while(num > 0){
                    if(Thread.currentThread().getName() == "A"){
                        System.out.println(Thread.currentThread().getName()+"卖出了"+(++i) +"张票，还剩余： "+(--num)+"张");
                        t.notifyAll();
                        try{
                            t.wait();
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }else{
                        System.out.println(Thread.currentThread().getName()+"卖出了"+(++i) +"张票，还剩余： "+(--num)+"张");
                        t.notifyAll();
                        try{
                            t.wait();
                        }catch(Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

}


